package tests;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.DropMode;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.TransferHandler;

public class DragAndDrop {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DragAndDrop window = new DragAndDrop();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DragAndDrop() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings("serial")
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
		frame.getContentPane().add(panel_1);
		
		final JList<String> list = new JList<String>();
		list.setDragEnabled(true);
		list.setDropMode(DropMode.ON);
		list.setTransferHandler(new TransferHandler() {
			
			@Override
			public int getSourceActions(JComponent c) {
				//to odpowiedni przycisk myszy (ew. użycie controla) określa typ akcji???
				//TODO tutaj wywolanie CopyOperation
				System.out.println("list getSource executed");
			    return COPY_OR_MOVE;
			}

			@Override
			protected Transferable createTransferable(JComponent c) {
				System.out.println("list createTr executed" + c);
				//TODO w normalnym programie mozna tutaj przesłać source component z którego pochodzi drag
			    return new StringSelection("jJJJ");
			}
			
			@Override
			protected void exportDone(JComponent c, Transferable t, int action) {
				System.out.println("list exportD executed");
			    if (action == MOVE) {
			        //c.removeSelection();
			    }
			}
			
			@Override
			public boolean canImport(TransferSupport info) {
				System.out.println("list canImport executed");
				return true;
			}
			
			//wywoływane w obiekcie na którym następuje drop
			@Override
			public boolean importData(TransferSupport info) {
				//TODO tutaj PasteOperation
				//wyciąganie folderu docelowego, można też tutaj wyciągać pliki źródłowe - podczas przeciągania pozostają one zaznaczone
				System.out.println("list importData executed");
				System.out.println(list.getSelectedValue());
				//if ((info.getTransferable().getTransferData(DataFlavor.stringFlavor))
				try {
					System.out.println(info.getTransferable().getTransferData(DataFlavor.stringFlavor));
					System.out.println("Where Dropped:" + info.getComponent() + "\n" + list);
				} catch (UnsupportedFlavorException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return true;
				
			}
			
		});
		panel_1.add(list);
		list.setModel(new AbstractListModel<String>() {
			String[] values = new String[] {"jeden", "dwa", "trzy", "cztery"};
			public int getSize() {
				return values.length;
			}
			public String getElementAt(int index) {
				return values[index];
			}
		});
		
		JLabel l1 = new JLabel("jeden");
		panel_1.add(l1);
		
		JLabel l2 = new JLabel("dwa");
		panel_1.add(l2);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel);
		
		JList<String> list_1 = new JList<String>();
		list_1.setDragEnabled(true);
		list_1.setDropMode(DropMode.ON);
		list_1.setTransferHandler(new TransferHandler() {
			
			@Override
			public int getSourceActions(JComponent c) {
				System.out.println("list_1 getSource executed");
			    return COPY_OR_MOVE;
			}

			@Override
			protected Transferable createTransferable(JComponent c) {
				System.out.println("list_1 createTr executed");
			    return new StringSelection("sadfdsf");
			}
			
			@Override
			protected void exportDone(JComponent c, Transferable t, int action) {
				System.out.println("list_1 exportD executed");
			    if (action == MOVE) {
			        //c.removeSelection();
			    }
			}
			
			@Override
			public boolean canImport(TransferSupport info) {
				System.out.println("list_1 canImport executed");
				return true;
			}
			
			@Override
			public boolean importData(TransferSupport info) {
				System.out.println("list_1 importData executed");
				System.out.println(list.getSelectedValue());
				try {
					System.out.println(info.getTransferable().getTransferData(DataFlavor.stringFlavor));
				} catch (UnsupportedFlavorException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return true;
				
			}
			
		});
		list_1.setModel(new AbstractListModel<String>() {
			String[] values = new String[] {"rjeden", "rdwa", "rtrzy", "rcztery"};
			public int getSize() {
				return values.length;
			}
			public String getElementAt(int index) {
				return values[index];
			}
		});
		panel.add(list_1);
		
		JLabel l3 = new JLabel("trzy");
		panel.add(l3);
		
		JLabel l4 = new JLabel("cztery");
		panel.add(l4);
	}

}
